local skel = fk.CreateSkill {
  name = "emo__bitafm",
  dynamic_desc = function (self, player, lang)
    if player:getMark("emo__bitafm_change") ~= 0 then
      return "emo__bitafm_inner"
    end
  end,
}

Fk:loadTranslationTable{
  ["emo__bitafm"] = "避他锋芒",
  [":emo__bitafm"] = "出牌阶段限一次，你可以拼点，若你没赢，与对方各摸两张牌；否则将「拼点」改为「视为使用【决斗】」并视为未发动此技能。",

  [":emo__bitafm_inner"] = "出牌阶段限一次，你可以视为使用【决斗】，若你没赢，与对方各摸两张牌。",
  ["#emo__bitafm0"] = "你可以拼点，若没赢，与对方各摸两张牌",
  ["#emo__bitafm1"] = "你可以视为使用【决斗】，若没赢，与对方各摸两张牌",

  ["$emo__bitafm1"] = "若有人敢敢向他叫板，那么很快就会明白这个决定有多愚蠢",
  ["$emo__bitafm2"] = "如果有谁要与他为敌，那么他的脑子一定有病",
}

skel:addEffect("active", {
  anim_type = "offensive",
  card_num = 0,
  min_target_num = 1,
  card_filter = Util.FalseFunc,
  prompt = function (self, player)
    return "#emo__bitafm" .. player:getMark("emo__bitafm_change")
  end,
  target_filter = function (self, player, to_select, selected)
    if player ~= to_select then
      if player:getMark("emo__bitafm_change") == 0 then
        return #selected == 0 and player:canPindian(to_select)
      else
        local card = Fk:cloneCard("duel")
        card.skillName = skel.name
        return card.skill:targetFilter(player, to_select, selected, {}, card)
      end
    end
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  times = function (self, player)
    return 1 - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:sortByAction(effect.tos)
    if player:getMark("emo__bitafm_change") == 0 then
      local to = effect.tos[1]
      local pd = player:pindian({to}, skel.name)
      if pd.results[to].winner == player then -- 拼点赢了
        room:setPlayerMark(player, "emo__bitafm_change", 1)
        player:clearSkillHistory(skel.name)
      else
        if not player.dead then
          player:drawCards(2, skel.name)
        end
        if not to.dead then
          to:drawCards(2, skel.name)
        end
      end
    else
      local card = Fk:cloneCard("duel")
      card.skillName = skel.name
      local use = { ---@type UseCardDataSpec
        from = player,
        tos = effect.tos,
        card = card,
      }
      room:useCard(use)
      for _, to in ipairs(effect.tos) do
        if not (use.damageDealt and use.damageDealt[to]) then -- 决斗没赢
          if not player.dead then
            player:drawCards(2, skel.name)
          end
          if not to.dead then
            to:drawCards(2, skel.name)
          end
        end
      end
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player:clearSkillHistory("emo__bitafm")
  player.room:setPlayerMark(player, "emo__bitafm_change", 0)
end)

return skel
